package com.dhq.myapp.widget

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatTextView
import androidx.core.content.ContextCompat
import com.dhq.myapp.R

/**
 * DESC
 * Created by DHQ on 2025/10/19.
 */
class SuperTextView : AppCompatTextView {
    /**
     * 简单的构造函数
     *
     * @param context View运行的Context环境
     */
    constructor(context: Context) : super(context)

    /**
     * inflate Xml布局文件时会被调用
     *
     * @param context View运行的Context环境
     * @param attrs   View在xml布局文件中配置的属性集合对象
     */
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)

    /**
     * 略
     *
     * @param context
     * @param attrs
     * @param defStyleAttr
     */
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    )


    override fun invalidateDrawable(drawable: Drawable) {
        invalidate()
    }


    override fun onDraw(canvas: Canvas) {
        drawDrawableBackground(canvas)
        super.onDraw(canvas)
    }


    @SuppressLint("NewApi")
    private fun drawDrawableBackground(canvas: Canvas) {
        //        实例化画笔对象
        val width = getWidth()
        val height = getHeight()
        val paint = Paint()
        //        给画笔设置颜色
        paint.setColor(ContextCompat.getColor(getContext(), R.color.color_1D3897))
        //        设置画笔属性
        paint.setStyle(Paint.Style.FILL) //画笔属性是实心圆

        //        paint.setStyle(Paint.Style.STROKE);//画笔属性是空心圆
//        paint.setStrokeWidth(8);//设置画笔粗细

        /*四个参数：
            参数一：圆心的x坐标
            参数二：圆心的y坐标
            参数三：圆的半径
            参数四：定义好的画笔
        */
        val r = (height * height + width * width) / (4 * height)
        val centerX = width / 2
        val centerY = -(r - height)

        canvas.drawCircle(centerX.toFloat(), centerY.toFloat(), r.toFloat(), paint)
        //        canvas.drawArc(0,0,width,height,30,90,false,paint);
    }
}